package com.metronoteapp.database;

import com.metronoteapp.model.GroupModel;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;

public class GroupNoteDao extends BaseDao {

	public static final String TABLE_NAME = "GroupNoteTbl";
	public static final String GROUP_ID = "GroupId";
	public static final String GROUP_NAME = "GroupName";
	public static final String COLOR = "Color";
	public static final String CREATE_TABLE = "CREATE TABLE " + TABLE_NAME
			+ "( " + GROUP_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
			+ GROUP_NAME + " TEXT," + COLOR + " TEXT)";
	public static final String[] COLUMNS = { GROUP_ID, GROUP_NAME, COLOR };

	public GroupNoteDao() {
	}

	public GroupNoteDao(Context _context) {
		super(_context);
	}

	// Insert new record
	public long insertGroup(GroupModel _group) {
		ContentValues cv = new ContentValues();
		cv.put(GROUP_NAME, _group.getGroupName());
		cv.put(COLOR, _group.getColor());
		return mDB.insert(TABLE_NAME, null, cv);
	}

	// Edit one record
	public long updateGroup(GroupModel _group) {
		ContentValues values = new ContentValues();
		String selection = GROUP_ID + " = '" + _group.getGroupId() + "'";
		values.put(GROUP_ID, _group.getGroupId());
		values.put(GROUP_NAME, _group.getGroupName());
		values.put(COLOR, _group.getColor());
		return mDB.update(TABLE_NAME, values, selection, null);
	}

	// Remove one record
	public boolean removeGroup(GroupModel _group) {
		return mDB.delete(TABLE_NAME, GROUP_ID + "=" + _group.getGroupId(),
				null) > 0;
	}

	// Select all record
	public Cursor getAllRecord() {
		return mDB.query(TABLE_NAME,
				new String[] { GROUP_ID, GROUP_NAME, COLOR }, null, null, null,
				null, GROUP_NAME);
	}

	// Select one record
	public Cursor getGroup(GroupModel _group) {
		Cursor mCursor = mDB.rawQuery("SELECT * FROM " + TABLE_NAME
				+ " WHERE TRIM(GroupId) = '" + _group.getGroupId() + "'", null);
		if (mCursor != null) {
			mCursor.moveToFirst();
		}

		return mCursor;
	}

	// Select one record
	public Cursor getGroupByName(GroupModel _group) {
		Cursor mCursor = mDB.rawQuery("SELECT * FROM " + TABLE_NAME
				+ " WHERE TRIM(GroupName) = '" + _group.getGroupName() + "'", null);
		if (mCursor != null) {
			mCursor.moveToFirst();
		}

		return mCursor;
	}
}
